package com.bestv.search.engine.compass.extend;

import org.compass.core.Compass;
import org.compass.core.CompassCallback;
import org.compass.core.CompassException;
import org.compass.gps.CompassGpsDevice;
import org.compass.gps.impl.SingleCompassGps;

public class SingleCompassGpsExtend extends SingleCompassGps {

    private volatile CompassTemplateExtend indexCompassTemplate;

    public SingleCompassGpsExtend(Compass compass, CompassGpsDevice[] devices) {
        super(compass);
        super.setGpsDevices(devices);
    }

    @SuppressWarnings({"unchecked", "rawtypes"})
    @Override
    public void executeForIndex(CompassCallback callback)
            throws CompassException {
        try {
            if (super.getIndexCompass() == null) {
                throw new IllegalStateException(
                        "executeForIndex is called outside of an index operation");
            }
            if (indexCompassTemplate == null)
                indexCompassTemplate = new CompassTemplateExtend(
                        super.getIndexCompass());
            indexCompassTemplate.execute(callback);
        } finally {
            indexCompassTemplate = null;
        }
    }
}
